﻿using System;
using System.Collections;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Services;
using System.Web.Services.Protocols;
using System.Web.SessionState;
using System.Xml.Linq;

namespace YXShop.Web.filehandle
{
    /// <summary>
    /// $codebehindclassname$ 的摘要说明
    /// </summary>
    [WebService(Namespace = "http://tempuri.org/")]
    [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
    public class product_auction_bidform : IHttpHandler, IRequiresSessionState 
    {

        public void ProcessRequest(HttpContext context)
        {
            YXShop.Common.SysParameter sp = new YXShop.Common.SysParameter();
            int UID = 0;
            if (context.Session["MemberID"] != null)
            {
                UID = Convert.ToInt32(context.Session["MemberID"].ToString());
            }
            else
            {
                ChangeHope.WebPage.Script.Alert("操作失败,您还未登陆！");
                context.Response.End();
            }

            string validate = ChangeHope.WebPage.PageRequest.GetFormString("txtCode") == null ? "" : ChangeHope.WebPage.PageRequest.GetFormString("txtCode");
            if (validate != "" && validate != null)
            {
                if (!HttpContext.Current.Request.Cookies["CheckCode"].Value.Equals(validate))
                {
                    HttpContext.Current.Response.Write("温馨提示：验证码错误");
                    HttpContext.Current.Response.End();
                }
            }

            int productid=ChangeHope.WebPage.PageRequest.GetFormInt("hfProductID");
            if (ChangeHope.WebPage.PageRequest.GetFormString("hfProductID") == "")
            {
                ChangeHope.WebPage.Script.Alert("操作失败,出价出现异常！");
            }
            YXShop.BLL.Product.ProductAuction bll = new YXShop.BLL.Product.ProductAuction();
            YXShop.Model.Product.ProductAuction model = bll.GetModelProductID(productid);
            decimal AddPrice = 0;
            decimal Price = 0;
            decimal Deposit = 0;
            if (model != null)
            {
                Price =decimal.Parse(model.Price.ToString());
                AddPrice = decimal.Parse(model.PriceRange.ToString());
                Deposit = decimal.Parse(model.Deposit.ToString());
            }
            
            YXShop.BLL.Product.Product_Auction_Bid pabll = new YXShop.BLL.Product.Product_Auction_Bid();
            YXShop.Model.Product.Product_Auction_Bid pamodel = new YXShop.Model.Product.Product_Auction_Bid();
            if (sp.AllowAuctionDeposit)
            {
                YXShop.BLL.Member.MemberAccount mabll = new YXShop.BLL.Member.MemberAccount();
                YXShop.Model.Member.MemberAccount mamoel = mabll.GetModel(UID);
                //查询用户余额
                if (mamoel != null)
                {
                    ChangeHope.DataBase.DataByPage dbp = pabll.GetList(" [order by] id asc", 2, " and productid=" + productid.ToString() + " and uid=" + UID.ToString() + "");
                    if (dbp.DataReader == null)
                    {
                        //查看用户余额是否足够缴纳保证金
                        if (mamoel.Capital >= Deposit)
                        {
                            YXShop.BLL.Member.UserInfoNote noteBll = new YXShop.BLL.Member.UserInfoNote();
                            YXShop.Model.Member.UserInfoNote noteModel = new YXShop.Model.Member.UserInfoNote();
                            noteModel.NoteName = "System";
                            noteModel.NoteType = 1;
                            noteModel.NoteDate = Convert.ToDateTime(System.DateTime.Now.ToShortDateString());
                            noteModel.Causation = "参加拍卖扣除保证金";
                            noteModel.BosomNote = "参加拍卖扣除保证金";
                            noteModel.BuckleOrAdd = 1;
                            noteModel.UserID = UID;
                            noteModel.Username = context.Session["MemberName"].ToString();
                            noteModel.TicketCount = Convert.ToDecimal(Deposit);

                            decimal deductPrice = Convert.ToDecimal(mamoel.Capital - Deposit);
                            if (mabll.Amend(UID, "Capital", deductPrice) != 0)
                            {
                                //记录数据
                               noteBll.Add(noteModel); 
                            }
                        }
                        else
                        {
                            ChangeHope.WebPage.Script.Alert("操作失败,帐户余额不足以缴纳保证金！");
                        }
                    }
                }
                else
                {
                    ChangeHope.WebPage.Script.Alert("操作失败,用户不存在！");
                }
            }
            string maxPrice=pabll.maxValues("price",productid.ToString());
            decimal DeMaxPrice=0;
            if(maxPrice!=string.Empty)
            {
                DeMaxPrice=decimal.Parse(maxPrice);
            }
            if (DeMaxPrice + AddPrice > decimal.Parse(ChangeHope.WebPage.PageRequest.GetFormString("txtPrice")))
            {
                ChangeHope.WebPage.Script.Alert("操作失败,出价金额小于当前拍卖金额,请重新出价！");
            }

            pamodel.productid = productid;
            pamodel.uid = UID.ToString();
            pamodel.orderno = string.Empty;
            pamodel.price =decimal.Parse(ChangeHope.WebPage.PageRequest.GetFormString("txtPrice"));
            pamodel.datetime = System.DateTime.Now;
            pamodel.count = ChangeHope.WebPage.PageRequest.GetFormInt("txtCount");
            pamodel.state = 0;
            pamodel.provinces = ChangeHope.WebPage.PageRequest.GetFormInt("Provinces");
            pamodel.city = ChangeHope.WebPage.PageRequest.GetFormInt("City");
            pamodel.borough = ChangeHope.WebPage.PageRequest.GetFormInt("borough");
            pamodel.address = ChangeHope.WebPage.PageRequest.GetFormString("txtAddress");
            pamodel.tel = ChangeHope.WebPage.PageRequest.GetFormString("txtTel");
            pamodel.phone = ChangeHope.WebPage.PageRequest.GetFormString("txtPhone");
            pamodel.zip = ChangeHope.WebPage.PageRequest.GetFormString("txtZip");
            pamodel.username = ChangeHope.WebPage.PageRequest.GetFormString("txtName");
            if (pabll.Add(pamodel) != 0)
            {
                ChangeHope.WebPage.Script.Alert("恭喜,您本次出价成功.");
            }
            else
            {
                ChangeHope.WebPage.Script.Alert("很遗憾,您本次出价失败！");
            }
        }

        public bool IsReusable
        {
            get
            {
                return false;
            }
        }
    }
}
